package com.jytc.framework.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.jytc.framework.bo.FwDeptBO;
import com.jytc.framework.commons.HtmlView;
import com.jytc.framework.commons.Page;
import com.jytc.framework.commons.PropertyFilter;
import com.jytc.framework.commons.ResultInfos;
import com.jytc.framework.constants.CommonConstants;
import com.jytc.framework.constants.RequestNameConstants;
import com.jytc.framework.constants.ResultConstants;
import com.jytc.framework.constants.UrlConstants;
import com.jytc.framework.dto.FwDeptDTO;
import com.jytc.framework.po.FwDept;
import com.jytc.framework.utils.HibernateWebUtils;

/**
 * @title: FwDeptController.java
 * @description: 部门管理
 * 
 * @author: dongyuese
 *
 */
@Controller
@RequestMapping("/admin/jsp/deptMgr.do")
public class FwDeptController
{
	/**
	 * 日志
	 */
	protected Logger logger = LoggerFactory.getLogger(getClass());

	/**
	 * 查询展示页面
	 */
	private final String QUERY_GOTO_URL = "/jytc/jsp/oa/sysMgr/deptMgr/dept_mgr.jsp";

	/**
	 * 编辑页面
	 */
	private final String EDIT_GOTO_URL = "/jytc/jsp/oa/sysMgr/deptMgr/dept_edit.jsp";

	/**
	 * 增加页面
	 */
	private final String ADD_GOTO_URL = "/jytc/jsp/oa/sysMgr/deptMgr/dept_add.jsp";

	/**
	 * 查询URL
	 */
	private final String VIEW_URL = "/admin/jsp/corpMgr.do?method=queryDept";

	/**
	 * 新增URL
	 */
	private final String ADD_URL = "/admin/jsp/corpMgr.do?method=addDept";

	/**
	 * 编辑URL
	 */
	private final String EDIT_URL = "/admin/jsp/corpMgr.do?method=editDept";

	/**
	 * 单位管理业务操作接口
	 */
	@Autowired
	private FwDeptBO fwDeptBO;

	/**
	 * 地区信息业务操作接口
	 */
	//@Autowired
	//private FSysRegionBO sysRegionBO;

	/**
	 * 单位信息查询
	 * @param request　请求对象
	 * @param model
	 * @return
	 */
	@RequestMapping(params = "method=queryDept")
	public ModelAndView queryDept(HttpServletRequest request, ModelMap model, Page page)
	{
		ResultInfos resultInfos = new ResultInfos();
		model.addAttribute(RequestNameConstants.RESULT_INFO, resultInfos);
		try
		{
			List<PropertyFilter> filters = HibernateWebUtils.buildPropertyFilters(request);
			Page pageResult = fwDeptBO.query(page, filters);

			model.addAttribute(RequestNameConstants.PAGE_OBJECT, pageResult);
		} catch (Exception e)
		{
			logger.error("部门查询异常", e);

			// 显示提示信息
			resultInfos.setPromptInfo(ResultConstants.getResultInfo(ResultConstants.QUERY_ERROR));
			resultInfos.setIsBack(false);
			resultInfos.setIsRedirect(false);
			return new ModelAndView(UrlConstants.INFORMATION_PAGE);
		}
		return new ModelAndView(QUERY_GOTO_URL);
	}

	/**
	 * 部门新增页面转向方法
	 * @param model
	 * @return
	 */
	@RequestMapping(params = "method=addDept")
	public ModelAndView addDept(ModelMap model)
	{
		ResultInfos resultInfos = new ResultInfos();
		model.addAttribute(RequestNameConstants.RESULT_INFO, resultInfos);
		try
		{
			List<FwDept> corpList = fwDeptBO.findByCorpType("deptType", CommonConstants.SYSTEM_TYPE_1);
//			List<FSysRegion> regionList = sysRegionBO.findByProperty("regionLevel", CommonConstants.REGION_LEVEL_CITY,
//					MatchType.EQ);
			model.addAttribute("corpList", corpList);
			//model.addAttribute("regionList", regionList);
		} catch (Exception e)
		{
			logger.error("部门新增初始化异常", e);

			// 显示提示信息
			resultInfos.setPromptInfo(ResultConstants.getResultInfo(ResultConstants.QUERY_ERROR));
			resultInfos.setIsBack(false);
			resultInfos.setIsRedirect(true);
			resultInfos.setGotoUrlForward(VIEW_URL);
			return new ModelAndView(UrlConstants.INFORMATION_PAGE);
		}
		return new ModelAndView(ADD_GOTO_URL);
	}

	/**
	 * 保存部门信息
	 * @param sysCorpDTO　部门信息数据传输对象
	 * @param modeld
	 * @return
	 */
	@RequestMapping(params = "method=saveDept")
	public ModelAndView saveDept(FwDeptDTO fwDeptDTO, ModelMap model)
	{
		ResultInfos resultInfos = new ResultInfos();
		model.addAttribute(RequestNameConstants.RESULT_INFO, resultInfos);
		try
		{
			int addFlag = fwDeptBO.save(fwDeptDTO);

			//判断保存是否成功
			if (addFlag == ResultConstants.INSERT_SUCCEED)
			{
				resultInfos.setPromptInfo(ResultConstants.getResultInfo(addFlag));
				resultInfos.setIsBack(false);
				resultInfos.setIsRedirect(true);
				resultInfos.setGotoUrlForward(ADD_URL);
			} else
			{
				resultInfos.setPromptInfo("部门名称重复");
				resultInfos.setIsBack(true);
			}
		} catch (Exception e)
		{
			logger.error("部门新增异常", e);

			// 显示提示信息
			resultInfos.setPromptInfo(ResultConstants.getResultInfo(ResultConstants.INSERT_ERROR));
			resultInfos.setIsBack(false);
			resultInfos.setIsRedirect(true);
			resultInfos.setGotoUrlForward(ADD_URL);
			return new ModelAndView(UrlConstants.INFORMATION_PAGE);
		}
		return new ModelAndView(UrlConstants.INFORMATION_PAGE);
	}

	/**
	 * 按照部门ID删除部门信息
	 * @param corpId　部门ID
	 * @param modeld
	 * @return
	 */
	@RequestMapping(params = "method=delDept")
	public ModelAndView delDept(Integer deptId, ModelMap model)
	{
		ResultInfos resultInfos = new ResultInfos();
		model.addAttribute(RequestNameConstants.RESULT_INFO, resultInfos);
		try
		{
			int delFlag = fwDeptBO.deleteById(deptId);

			//判断删除是否成功
			if (delFlag == ResultConstants.DELETE_SUCCEED)
			{
				resultInfos.setPromptInfo(ResultConstants.getResultInfo(delFlag));
				resultInfos.setIsBack(false);
				resultInfos.setIsRedirect(true);
				resultInfos.setGotoUrlForward(VIEW_URL);
			} else if (delFlag == CommonConstants.CORP_USER_QUOTED)
			{
				resultInfos.setPromptInfo("部门已被用户引用，不能删除");
				resultInfos.setIsBack(true);
			} else if (delFlag == CommonConstants.IS_EXIST_CHILD_CORP)
			{
				resultInfos.setPromptInfo("存在下级部门，不能删除");
				resultInfos.setIsBack(true);
			}
		} catch (Exception e)
		{
			logger.error("部门删除异常", e);

			// 显示提示信息
			resultInfos.setPromptInfo(ResultConstants.getResultInfo(ResultConstants.DELETE_ERR0R));
			resultInfos.setIsBack(false);
			resultInfos.setIsRedirect(true);
			resultInfos.setGotoUrlForward(VIEW_URL);
			return new ModelAndView(UrlConstants.INFORMATION_PAGE);
		}
		return new ModelAndView(UrlConstants.INFORMATION_PAGE);
	}

	/**
	 * 批量删除部门信息
	 * @param corpId　部门ID集合
	 * @param modeld
	 * @return
	 */
	@RequestMapping(params = "method=delDepts")
	public ModelAndView delDepts(Integer[] deptIds, ModelMap model)
	{
		ResultInfos resultInfos = new ResultInfos();
		model.addAttribute(RequestNameConstants.RESULT_INFO, resultInfos);
		try
		{
			int delFlag = fwDeptBO.deleteByIds(deptIds);

			//判断删除是否成功
			if (delFlag == ResultConstants.DELETE_SUCCEED)
			{
				resultInfos.setPromptInfo(ResultConstants.getResultInfo(delFlag));
				resultInfos.setIsBack(false);
				resultInfos.setIsRedirect(true);
				resultInfos.setGotoUrlForward(VIEW_URL);
			} else
			{
				resultInfos.setPromptInfo(ResultConstants.getResultInfo(ResultConstants.DELETE_FAILED));
				resultInfos.setIsBack(true);
			}
		} catch (Exception e)
		{
			logger.error("部门批量删除异常", e);

			// 显示提示信息
			resultInfos.setPromptInfo(ResultConstants.getResultInfo(ResultConstants.DELETE_ERR0R));
			resultInfos.setIsBack(false);
			resultInfos.setIsRedirect(true);
			resultInfos.setGotoUrlForward(VIEW_URL);
			return new ModelAndView(UrlConstants.INFORMATION_PAGE);
		}
		return new ModelAndView(UrlConstants.INFORMATION_PAGE);
	}

	/**
	 * 部门修改初始化页面
	 * @param corpId　部门ID
	 * @param modeld
	 * @return
	 */
	@RequestMapping(params = "method=editDept")
	public ModelAndView editDept(Integer deptId, ModelMap model)
	{
		ResultInfos resultInfos = new ResultInfos();
		model.addAttribute(RequestNameConstants.RESULT_INFO, resultInfos);
		try
		{
			FwDeptDTO fwDeptDTO = fwDeptBO.findById(deptId);
			List<FwDept> corpList = fwDeptBO.findByCorpType("corpType", CommonConstants.SYSTEM_TYPE_1);
			//List<FSysRegion> regionList = sysRegionBO.findAll();
			model.addAttribute("corpList", corpList);
			//model.addAttribute("regionList", regionList);
			model.addAttribute(RequestNameConstants.RESULT_OBJECT, fwDeptDTO);
		} catch (Exception e)
		{
			logger.error("部门编辑异常", e);

			// 显示提示信息
			resultInfos.setPromptInfo(ResultConstants.getResultInfo(ResultConstants.EDIT_ERROR));
			resultInfos.setIsBack(false);
			resultInfos.setIsRedirect(true);
			resultInfos.setGotoUrlForward(VIEW_URL);
			return new ModelAndView(UrlConstants.INFORMATION_PAGE);
		}
		return new ModelAndView(EDIT_GOTO_URL);
	}

	/**
	 * 修改部门信息
	 * @param sysCorpDTO　部门信息数据传输对象
	 * @param model
	 * @return
	 */
	@RequestMapping(params = "method=updDept")
	public ModelAndView updDept(FwDeptDTO fwDeptDTO, ModelMap model)
	{
		ResultInfos resultInfos = new ResultInfos();
		model.addAttribute(RequestNameConstants.RESULT_INFO, resultInfos);
		try
		{
			int updFlag = fwDeptBO.update(fwDeptDTO);

			//判断修改是否成功
			if (updFlag == ResultConstants.UPDATE_SUCCEED)
			{
				resultInfos.setPromptInfo(ResultConstants.getResultInfo(updFlag));
				resultInfos.setIsBack(false);
				resultInfos.setIsRedirect(true);
				resultInfos.setGotoUrlForward(VIEW_URL);
			} else if (updFlag == CommonConstants.CORP_IS_EXIST)
			{
				resultInfos.setPromptInfo("部门名称重复");
				resultInfos.setIsRedirect(true);
				resultInfos.setGotoUrlForward(EDIT_URL + "&corpId=" + fwDeptDTO.getDeptId());
				return new ModelAndView(UrlConstants.INFORMATION_PAGE);
			} else if (updFlag == CommonConstants.CORP_USER_QUOTED)
			{
				resultInfos.setPromptInfo("部门已被用户引用，不能设置为无效");
				resultInfos.setIsRedirect(true);
				resultInfos.setGotoUrlForward(EDIT_URL + "&corpId=" + fwDeptDTO.getDeptId());
				return new ModelAndView(UrlConstants.INFORMATION_PAGE);
			}
		} catch (Exception e)
		{
			logger.error("部门修改异常", e);

			// 显示提示信息
			resultInfos.setPromptInfo(ResultConstants.getResultInfo(ResultConstants.UPDATE_ERROR));
			resultInfos.setIsBack(false);
			resultInfos.setIsRedirect(true);
			resultInfos.setGotoUrlForward(EDIT_URL + "&corpId=" + fwDeptDTO.getDeptId());
			return new ModelAndView(UrlConstants.INFORMATION_PAGE);
		}
		return new ModelAndView(UrlConstants.INFORMATION_PAGE);
	}

	/**
	 * 根据部门ID查询对应区域范围信息
	 * @param request　请求对象
	 * @return
	 */
	@RequestMapping(params = "method=queryRegion")
	public ModelAndView queryRegion(HttpServletRequest request)
	{
		ModelAndView mav = null;
		try
		{
			String regionInfo = fwDeptBO.getRegionByCorpId(Integer.valueOf(request.getParameter("corpId")));
			mav = new ModelAndView(HtmlView.instance);
			mav.addObject(HtmlView.HTML_RESULT, regionInfo);
		} catch (Exception e)
		{
			logger.error("根据部门ID查询对应区域范围信息", e);
		}
		return mav;
	}

}
